//
//  ForgotPasswordViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/22.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "ForgotPasswordViewController.h"
#import "MZTimerLabel.h"

@interface ForgotPasswordViewController ()<MZTimerLabelDelegate>
@property (weak, nonatomic) IBOutlet UIButton *agreeBtn;

@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@property (weak, nonatomic) IBOutlet UIButton *getValidateCode;
@property (nonatomic, strong) NSString *yanZhengMa;
@property (weak, nonatomic) IBOutlet UITextField *yanzhengma;

@property (weak, nonatomic) IBOutlet UITextField *phoneNum;//手机号
@property (nonatomic, strong) NSTimer *timer;;

@end

static NSInteger countDownSeconds;

@implementation ForgotPasswordViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"找回登录密码";
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    
     self.agreeBtn.selected = YES;//默认为选中
    
    //创建定时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(getValidateCodeCountDown) userInfo:nil repeats:YES];
    [_timer setFireDate:[NSDate distantFuture]];
    
}
- (IBAction)agreeBtn:(UIButton *)sender {
    if (sender.selected) {
        sender.selected = NO;
        _sureBtn.enabled = NO;
    }else{
        sender.selected = YES;
        _sureBtn.enabled = YES;
    }
}


- (IBAction)sendValidateCodeClicked:(UIButton *)sender {
    
    if (self.phoneNum.text.length >= 11) {
        if (!sender.selected) {
            //设置倒计时时间为60妙
            countDownSeconds = 60;
            _getValidateCode.selected = YES;
            _getValidateCode.enabled = NO;
            _getValidateCode.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1.00];
            
            //打开定时器
            [_timer setFireDate:[NSDate distantPast]];
            
            
            NSDictionary *dict = @{
                                   @"Tel":self.phoneNum.text,
                                   @"OrderType":@"0",
                                   @"OrderFrom":@"1"
                                   };
            [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Api.GetMsm" withParaments:dict withSuccessBlock:^(NSDictionary *object) {
                NSLog(@"%@", object);
                //{VerificationCode:245301}
                NSDictionary *dic = [object[@"Data"] firstObject];
                self.yanZhengMa = dic[@"VerificationCode"];
            } withFailureBlock:^(NSError *error) {
                NSLog(@"%@", error);
                [ToastView showErrorWithStatus:@"网络请求失败"];
            } progress:^(float progress) {
                
            }];
        }
    }else{
        [ToastView showInfoWithStatus:@"请输入手机号"];
    }
    
}

//定时器每隔一秒调用的方法
- (void)getValidateCodeCountDown
{
    if (countDownSeconds >= 0) {
        [_getValidateCode setTitle:[NSString stringWithFormat:@"剩余%lds",(long)countDownSeconds] forState:UIControlStateNormal];
        countDownSeconds--;
    }else{
        countDownSeconds = 60;
        _getValidateCode.selected = NO;
        _getValidateCode.enabled = YES;
        _getValidateCode.backgroundColor = [UIColor colorWithRed:0.93 green:0.37 blue:0.31 alpha:1.00];
        [_getValidateCode setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_timer setFireDate:[NSDate distantFuture]];
    }
}




- (IBAction)sureBtn:(id)sender {
    
    if ([_yanzhengma.text isEqualToString:@""]) {
        [ToastView showInfoWithStatus:@"请输入验证码"];
    }else if([_Password.text isEqualToString:@""]){
        [ToastView showInfoWithStatus:@"请输入新密码"];
    }else if([_erPassword.text isEqualToString:@""]){
        [ToastView showInfoWithStatus:@"请再次输入新密码"];
    }else if(![_erPassword.text isEqualToString:_Password.text]){
        [ToastView showInfoWithStatus:@"两次密码不一致, 请重新输入"];
    }else{
        
        [self changePassword];
        [[NSUserDefaults standardUserDefaults] setObject:_Password.text forKey:@"Password"];
        

        
    }

    
}


//修改密码接口
- (void)changePassword{
    
    
    
    //    http://test.open.wanyuanfu.net/api/user/openapi   User.GetUpdatePwd
    
    
    NSDictionary *para = @{
                           @"LoginName":_phoneNum.text,
                           @"NewPassword":_Password.text
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"User.GetUpdatePwd" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        
        [ToastView showInfoWithStatus:object[@"Message"]];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
        
    } progress:nil];

    
}


- (void)dealloc{
    [_timer invalidate];
}


//空白去键盘

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{
    
    [self.view.window endEditing:YES];
    
}






@end
